1775C - Interesting Sequence - CodeForces Solution


bitmasks math

Please click on ads to support us..

Python Code:

def find_m(n, x):
    f = [0] * 60
    for i in range(60):
        if (~n >> i) & 1:
            f[i] = n
        else:
            f[i] = (n & ~((1 << i) - 1)) + (1 << i)

    m = n
    for i in range(60):
        if (~x >> i) & 1:
            m = max(m, f[i])

    for i in range(60):
        if (x >> i) & 1:
            if m >= f[i]:
                return -1
    return m

def main():
    t = int(input())
    for i in range(t):
        n, x = map(int, input().split())
        result = find_m(n, x)
        print(result)

if __name__ == '__main__':
    main()

C++ Code:

//Rakib Hasan

#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using    namespace __gnu_pbds;
using    namespace std;
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define optimize()  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define mod  998244353 //1000000007
const long long infll=9000000000000000000;
const int inf = 2000000000;
const double eps=1e-9;


#define                    ll                   long long
#define                    pa                   pair<int,int>
#define                    ff                   first
#define                    ss                   second
#define                    pb                   push_back
#define                    PI                   acos(-1.0)
#define                    vi                   vector<int>
#define                    vll                  vector<ll>
#define                    yo                   cout<<"YES"<<endl
#define                    no                   cout<<"NO"<<endl
#define                    done                 cout<<ans<<endl
#define                    mem(a,b)             memset(a,b,sizeof(a))
#define                    minus                cout<<"-1"<<endl
#define                     duck                cout<<0<<endl
#define                    sqr(a)               ((a)*(a))
#define                    f(i,a,b)             for(int i=a;i<=(b);i++)
#define                    r(i,a,b)             for(int i=a;i>=(b);i--)
#define                    all(v)               v.begin(),v.end()
#define                    vpush(v,x)           int x; cin>>x; v.push_back(x);
#define                    mid(s,l)             (s+(l-s)/2)



//#define int long long


void solve()
{
   ll n,x;
   cin>>n>>x;
   if(n<x){
    cout<<-1<<endl;
    return;
   }
   if(n==x){
    cout<<n<<endl;
    return;
   }
   if(x==0){
    cout<<(1LL<<((ll)log2(n)+1LL))<<endl;
   }
   
   bitset<66>bn(n);
   bitset<66>bx(x);
   bool check=false;

   for(int i=0;i<=65;i++){
    if(bn[i]==1){
        if(bx[i]==0 and check){
            cout<<-1<<endl;
            return;
        }
        else if(bx[i]==1)check=true;
    }
    else if(bx[i]==1)check =true;
   }
    ll num=0;
    ll ans=n;
    ll last;


   for(int i=0;i<=65;i++){
    if(bx[i]==1){
        if(last==(1LL<<(ll)i)){
            cout<<-1<<endl;
            return;
        }
        ans+=(last-num);
        cout<<ans<<endl;
        return;
    }
    if(bn[i]==1){
        num+=(1LL<<(ll)i);
        last=(1LL<<((ll)i+1LL));

    }
   }


}

//signed main()
int main()
{
    optimize();
    int t;
    cin>>t;
    while(t--)
    {
     solve();
    }
}


Comments

Submit
0 Comments
More Questions

1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle